﻿//
// Copyright 2008-2011 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//

using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.Serialization;

namespace YaTools.Yaml
{
#if SILVERLIGHT
[assembly: DynamicLanguageProvider(typeof(YamlLanguageContext), YamlLanguageContext.YamlDisplayName, YamlLanguageContext.YamlNames, YamlLanguageContext.YamlFileExtensions)]
#endif

    /// <summary>
    /// Public utility class for using YAML
    /// </summary>
    [ContractVerification(false)]
    public static class YamlLanguage
    {
        /// <summary>
        /// Converts YAML text into a .NET object graph
        /// </summary>
        /// <typeparam name="T">Desired type of result</typeparam>
        /// <param name="text">YAML text</param>
        /// <returns>.NET Object graph of type T constructed from text</returns>
        /// <remarks>
        /// If the text contains several YAML documents, only the first 
        /// non-hidden document will be returned.   Hidin
        /// g hasn't been implemented
        /// yet, so this effectively means the first document will be returned.
        /// </remarks>
        public static T StringTo<T>(string text)
        {
            return Deserialize<T>(text);
        }

        public static object StringTo(string text)
        {
            return StringTo<object>(text);
        }

        /// <summary>
        /// Converts YAML text into a .NET object graph
        /// </summary>
        /// <typeparam name="T">Desired type of result</typeparam>
        /// <param name="text">YAML text</param>
        /// <param name="path">Path name for use in error messages</param>
        /// <returns>.NET Object graph of type T constructed from text</returns>
        /// <remarks>
        /// If the text contains several YAML documents, only the first 
        /// non-hidden document will be returned.   Hiding hasn't been implemented
        /// yet, so this effectively means the first document will be returned.
        /// </remarks>
        public static T StringTo<T>(string text, string path)
        {
            return Deserialize<T>(text);
        }

        public static object StringTo(string text, string path)
        {
            return StringTo<object>(text, path);
        }


        /// <summary>
        /// Converts YAML text into a .NET object graph
        /// </summary>
        /// <typeparam name="T">Desired type of result</typeparam>
        /// <param name="path">Path name of file to read from</param>
        /// <returns>.NET Object graph of type T constructed from text</returns>
        /// <remarks>
        /// If the file contains several YAML documents, only the first 
        /// non-hidden document will be returned.   Hiding hasn't been implemented
        /// yet, so this effectively means the first document will be returned.
        /// </remarks>
        public static T FileTo<T>(string path)
        {
            Contract.Requires(path != null);

            using (var reader = File.OpenText(path))
            {
                return Deserialize<T>(reader);
            }
        }

        public static object FileTo(string path)
        {
            return FileTo<object>(path);
        }

        public static ITaggedObject RepresentFile(string path)
        {
            return FileTo<ITaggedObject>(path);
        }

        internal static T Deserialize<T>(string str)
        {
            var target = new TaggedObjectSink();
            using (var parser = new YamlParser(target))
            {
                try
                {
                    parser.SetSource(str);
                    parser.ParseDocument();
                    Contract.Assume(parser.Load != null);
                    Contract.Assume(parser.Load.Object != null);
                    return (T) parser.Load.Object;
                }
                catch (SerializationException ex)
                {
                    string place = string.Format("{0}({1}) : {2}",
                                                 "<string>", parser.TokenSpan, ex.Message);
                    throw new SerializationException(place, ex);
                }
            }
        }

        internal static T Deserialize<T>(TextReader reader)
        {
            var target = new TaggedObjectSink();
            using (var parser = new YamlParser(target))
            {
                try
                {
                    parser.SetSource(reader);
                    parser.ParseDocument();
                    Contract.Assume(parser.Load != null);
                    Contract.Assume(parser.Load.Object != null);
                    return (T) parser.Load.Object;
                }
                catch (SerializationException ex)
                {
                    string place = string.Format("{0}({1}) : {2}",
                                                 "<string>", parser.TokenSpan, ex.Message);
                    throw new SerializationException(place, ex);
                }
            }
        }
    }
}